#include <iostream>
#include <regex>
#include <string>
#include <unistd.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <iomanip>
#include <string.h>

#include <stdint.h>

void little_big()
{
    uint32_t test = 0x12345678; // 一个包含多个字节的整数

    // 将整数的地址转换为 char 类型的指针，然后检查第一个字节        //低地址存12是大端
    if (*(reinterpret_cast<char *>(&test)) == 0x12)
    {
        std::cout << "Big-endian machine" << std::endl;
    }
    else if (*(reinterpret_cast<char *>(&test)) == 0x78)
    {
        std::cout << "Little-endian machine" << std::endl;
    }
    else
    {
        std::cout << "Neither little-endian nor big-endian?" << std::endl;
    }
}

void testFork()
{
    pid_t pid = ::fork();
    if (pid == 0)
    {
        printf("我是子进程，我结束了！");
        std::cout << "Child process with PID: " << getpid() << std::endl;
        // sleep(60);
        int a = 10;
        a = a / 0;
        exit(10);
    }
    int status;
    int ret = ::waitpid(0, &status, WUNTRACED);
    if (ret > 0 && (status & 0X7F) == 0)
    { // 正常退出
        printf("child exit code:%d\n", (status >> 8) & 0XFF);

        return;
    }
    else
    {
        printf("被信号所杀，信号为:%d\n", (status & 0x7F));
        printf("core_dump标志为:%d\n", ((status >> 7) & 0x01)); // 判断当前信号是否是属于core_dump
    }
}

// 在大端序中，高位字节存储在低地址，低位字节存储在高地址   更符合人类的阅读习惯
int main()
{

    int num = 1;

    // 将整数转换为网络字节序  ,
    int net_num = htonl(num);                          // 01 00 00 00 从左向右是低到高地址   现在变成大端中的1 那么就是 00 00 00 01
    printf("Hexadecimal representation: %08X\n", num); // 如果按照大端打，看作存储的就是 00 00 00 01 那么结果应该是 00 00 00 01
    std::cout << net_num << std::endl;
    printf("Hexadecimal representation: %08X\n", net_num); // 如果按照大端打，看作存储的就是 01 00 00 00 那么结果应该是01 00 00 00

    const char *p = (const char *)&net_num;
    int a = *p;
    std::cout << "a: " << a << " ";
    // 打印变量 p 指向的地址
    std::cout << "Address of p: " << std::hex << (uintptr_t)p << std::endl;
    for (int i = 0; i < 3; i++)
    {
        p++;
        std::cout << "Address of p: " << std::hex << (uintptr_t)p << std::endl;
        int a = *p;
        std::cout << a << " ";
    }
    std::cout << std::endl;

    // 将网络字节序的整数转换为字节序列
    std::string net_bytes((const char *)&net_num, sizeof(net_num));

    // 打印结果

    std::cout << "Original number: " << num << std::endl;
    std::cout << "Network bytes: ";
    for (size_t i = 0; i < net_bytes.size(); ++i)
    {
        std::cout << std::hex << (int)(unsigned char)net_bytes[i] << " ";
    }
    std::cout << std::endl;

    /////此时验证整数0直接存储在std::string中，存入buffer中会出现截断
    char buffer_string[1024];
    memcpy(buffer_string, net_bytes.data(), net_bytes.size());
    std::cout << "开始实验: " << std::endl;
    std::cout << buffer_string << std::endl;

    char buffer1[1024]; // 此时脏内存，不会初始化          验证当第一个字符为'0'字符会正常答应，而如果是一个整数0的话就会截断
    buffer1[0] = '0';
    buffer1[1] = 0;
    buffer1[2] = '1';

    char buffer[1024]; // 此时脏内存，不会初始化
    buffer[0] = '1';
    buffer[1] = '2';
    buffer[2] = '0';
    // buffer[2] = 0;
    std::string mm;
    std::string mm1;
    mm += buffer;
    mm1 += buffer1;

    std::cout << "mm: " << mm.c_str() << std::endl;
    std::cout << "mm: " << mm << std::endl;

    std::cout << "mm1: " << mm1.c_str() << std::endl;
    std::cout << "mm1: " << mm1 << std::endl;

    /*if(status&0x80 == 1)
    {
        printf("子进程被暂停");
    }*/
    return 0;
}